package is.sclab;

import is.sclab.datastore.DataStore;
import is.sclab.datastore.DataStoreDao;

import java.io.IOException;
import java.io.Writer;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@SuppressWarnings("serial")
public class RetrieveByParm extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html");
		response.setCharacterEncoding("utf-8");

		String name = request.getParameter("name");

		//PersistenceManager pm = PMF.get().getPersistenceManager();
		String query = "select from " + DataStore.class.getName()+" where name == '"+name+"'";
		//List<DataStore> store = (List<DataStore>) pm.newQuery(query).execute();
		DataStoreDao dao = new DataStoreDao();
		List<DataStore> store = dao.getDataStore(name);//String searchword
		
	    Writer out = response.getWriter();
	    out.write("<?xml version=\"1.0\" encoding=\"utf-8\" ?>");
	    out.write("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\">");
	    out.write("<head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">");
	    out.write("<body>");
	    out.write("<table border=\"1\">");
	    out.write("<tr><th>ID</th><th>DataName</th><th>DataValue</th><th>Date</th></tr>");
	    
	    for(DataStore data : store){
		    out.write("<tr>");
		    out.write("<td>" + data.getId() + "</td>");
		    out.write("<td>" + data.getName() + "</td>");
		    out.write("<td>" + data.getData() + "</td>");
		    out.write("<td>" + data.getDate() + "</td>");
		    out.write("</tr>");
	    }
	    
	    out.write("</table>");
	    out.write("<p>"+query+"</p>");
	    out.write("<a href=gaRetrieve_db>show current table</a><br/>");
	    out.write("<a href=\"/index.html\">top</a>");
	    out.write("</body></html>");
	}
}
